# _*_ coding : utf-8 _*_
# @Time : 2023/3/15 0015 17:16
# @Author : 菜鸟王小二
# @File : 15_urllib_代理
# @Project : python爬虫

import urllib.request

url = 'http://www.baidu.com/s?wd=ip%E5%9C%B0%E5%9D%80%E6%9F%A5%E8%AF%A2'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.63'
}
request = urllib.request.Request(url=url,headers=headers)

# 快代理网站找个ip
proxies = {
    'http':'182.34.102.50:9999'
}
# 使用代理，参数为代理的ip,字典形式，使用代理以防止频繁访问本地ip被封
handler = urllib.request.ProxyHandler(proxies)

opener = urllib.request.build_opener(handler)

response = opener.open(request)

content = response.read().decode('utf-8')

print(content)

with open('proxy.html','w',encoding='utf-8') as fp:
    fp.write(content)



